#pragma once

#include "../Templates/BuilderBase.h"
#include "../Core/CoreGlobals.h"
#include "VulkanRHI.h"

class RenderProcess;

class RHIResource
{
	friend class RenderProcess;
public:
	RHIResource() : NumRefs(0), mProcess(nullptr) {}
	virtual ~RHIResource() { assert(NumRefs.GetValue() == 0); }
	/**
	 *	Increases the reference count
	 *	@return the new reference count
	**/
	uint32 AddRef()
	{
		return uint32(NumRefs.Increment());
	}
	/**
	 *	Decreases the reference count and destroys the graph event if it is zero.
	 *	@return the new reference count
	**/
	uint32 Release()
	{
		uint32 Refs = uint32(NumRefs.Decrement());

		if (Refs == 0)
		{
			ReleaseResource();
		}
		return Refs;
	}
	uint32 GetRefCount() const
	{
		return uint32(NumRefs.GetValue());
	}

	virtual void ReleaseResource() {};

private:

	void ClearProcess();

	void SetProcessAndName(RenderProcess* process, const std::string& name);

private:

	/** Number of outstanding references to this graph event **/
	mutable ThreadCounter NumRefs;

protected:

	RenderProcess* mProcess;

	std::string mName;
};